package com.shujia.sql

import org.apache.spark.sql.{DataFrame, SparkSession}

object Demo9MapJoin {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession
      .builder()
      .master("local")
      .appName("student")
      .config("spark.sql.shuffle.partitions", 1)
      .getOrCreate()

    import spark.implicits._
    import org.apache.spark.sql.functions._


    //读取学生表
    val student: DataFrame = spark
      .read
      .format("csv")
      .option("sep", ",")
      .schema("id STRING,name STRING,age INT,gender STRING, clazz STRING")
      .load("data/students.txt")

    //读取分数表
    val score: DataFrame = spark.read
      .format("csv")
      .option("sep", ",")
      .schema("sId STRING,cId STRING ,sco DOUBLE")
      .load("data/score.txt")


    /**
      * DF实现mapjoin
      * //将小表广播实现mapjoin
      * .hint("broadcast")
      *
      */


    val joinDF: DataFrame =
      score.join(student.hint("broadcast"), $"sId" === $"id")


    joinDF.show()


    while (true) {

    }
  }
}
